﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeSoft.Application.Bussiness;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util; 

namespace LeSoft.Application.Web.Areas.QualificationManage.Controllers
{
    [HandleError]
    public class MemberController : MvcControllerBase
    {

        private MemberBLL bll = new MemberBLL();
        private QualificationBillBLL QFBbll = new QualificationBillBLL();
        //
        // GET: /QualificationManage/Member/
        #region 视图
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit, "会员", "")] 
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.Visit, "会员详情修改", "")] 
        public ActionResult Form()
        {
            ViewBag.Mode = new CodeRuleBLL().CheckBillCodeMode(((int)CodeRuleEnum.Member).ToString());
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit, "会员详情查看", "")] 
        public ActionResult Details()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit, "会员详情初审", "")] 
        public ActionResult FirstTrialDetails()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit, "会员详情复审", "")] 
        public ActionResult RetrialDetails()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit, "会员详情合同", "")] 
        public ActionResult ContractDetails()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit, "会员详情看房", "SearchRoomDetails")] 
        public ActionResult SearchRoomDetails()
        {
            return View();
        }
        #endregion

        #region 查询数据
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit, "会员详情", "GetFormJson")] 
        public ActionResult GetFormJson(string keyValue)
        {
            var data = bll.GetEntity(keyValue);
            return ToJsonResult(data);
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.Visit, "", "GetPStructLsit")] 
        public ActionResult GetPStructLsit(string MemberId)
        {
            var dataRelation = bll.GetRelationWPList().Where(t => t.MemberId == MemberId).ToList();
            var dataPstruct = new PStructBLL().GetList().Where(t => t.Type == 5).ToList();
            var dataCustomer = new CustomerInfoBLL().GetList().ToList();
            var data = from a in dataRelation
                       join b in dataPstruct on a.PStructId equals b.Id
                       join c in dataCustomer on a.CustomerId equals c.Id
                       select new
                       {
                           a.Id,
                           a.PStructId,
                           a.MemberId,
                           a.CustomerId,
                           a.AccountCode,
                           a.IsDefault,
                           a.Type,
                           a.EnabledMark,
                           a.CreateDate,
                           a.ModifyDate,
                           PStructName = b.AllName,
                           CustomerName = c.Name
                       };

            return Content(data.ToJson());
        }


        /// <summary>
        /// 分页获取获取单据列表
        /// </summary>
        /// <param name="type"> 1-初审 2-复审</param>
        /// <param name="pagination">分页参数</param>
        /// <param name="queryJson">  查询参数json:State StartTime EndTime Name IDCard Phone</param>
        /// <returns></returns>

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList)]
        public ActionResult GetBillPageList(Pagination pagination, string queryJson)
        {
            var data = QFBbll.GetPageList(3, pagination, queryJson);
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.GetList)]
        public ActionResult GetListJson(Pagination pagination, string queryJson)
        {
            var watch = CommonHelper.TimerStart();
            var data = bll.GetPageList(pagination, queryJson);
            var JsonData = new
            {
                rows = data,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                costtime = CommonHelper.TimerEnd(watch)
            };
            return Content(JsonData.ToJson());
        }
        #endregion

        #region 保存数据
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult RemoveForm(string keyValue)
        {
            if (bll.GetRelationWPList().Where(t => t.MemberId == keyValue && t.EnabledMark == 1).Count() > 0)
            {
                return Error("请先解除房间绑定。");
            }
            else
            {
                bll.RemoveForm(keyValue);
                return Success("删除成功。");
            }

        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SaveForm(string keyValue, string member, string pstructlist)
        {
            MemberInfoEntity entity = member.ToObject<MemberInfoEntity>();
            List<RelationWPEntity> list = pstructlist.ToObject<List<RelationWPEntity>>();
            bll.SaveForm(keyValue, entity, list);
            return Success("操作成功。");
        }

        #endregion
    }
}
